package caculate.sliding_window;

/**
 * @program: rocsun
 * @description:
 * @author: yx.guo
 * @create: 2025-03-02 22:53
 **/
public class MinLengthSubArr {

    public static int minSubArrayLen(int s, int[] nums) {
        int length = nums.length;
        int minArrLength = length + 1; // 给个默认最大值，代替正无穷
        int sum = 0;
        int start = 0;
        for (int end = 0; end < length; end++) {
            sum += nums[end];
            // 这里计算的最小长度的时候，如果满足条件才去求长度
            if (sum >= s) minArrLength = Math.min(minArrLength, end - start + 1);
            // 加完之后如果大就一直减开头
            while (sum - nums[start] >= s) {
                sum -= nums[start++];
                minArrLength = Math.min(minArrLength, end - start + 1);
            }
        }

        return minArrLength < length + 1 ? minArrLength : 0;
    }

    public static void main(String[] args) {
        int[] nums = {2,3,1,2,4,3};
        int s = 7;
        System.out.println(minSubArrayLen(s, nums));
    }

}
